﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
    <title>发送新信件</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageStyles" runat="server">
    <style type="text/css">
        #divReceiver
        {
            border: solid 1px #ccc;
            margin-top: -1px;
            margin-bottom: 3px;
            border-radius: 4px;
        }
        #divReceiver
        {
        }
        #divReceiver ul
        {
            overflow: hidden;
            text-align: center;
        }
        #divReceiver li
        {
            clear: both;
        }
        #divReceiver ul li p
        {
            float: left;
            min-width: 100px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageScripts" runat="server">
    <link href="../../Content/userInfoSelect.css" rel="stylesheet" type="text/css" />
    <script src="../../Content/xheditor/xheditor-zh-cn.js" type="text/javascript"></script>
    <script type="text/javascript">

        //************* URL常量

        var URL_MESSAGE_SEND = '/Message/DoSend';

        //************* 全局变量

        var g_Var = {
            'currentTime': null,
            'currentUser': null,
            'recieverId': 'null'

        };

        //************* 常量定义

        //************* Accessor访问器

        var EzAccessor = {};

        if (ezmock.IS_MOCKED) {


        } else {
            var realEzAccessor = {

                'doSendMessage': function () {
                    //获得标题
                    var title = $('#messageTitle').val();
                    if ($('#messageTitle').defaultText()) {
                        title = "";
                    }
                    //获得内容
                    var text = $('#messageContent').val();

                    //获得是否需要确认
                    var needConfirm = $('#chk-need-confirm').attr("checked");


                    //获得收件人名单
                    var memberString = g_Var['recieverId'];

                    //发送信件
                    $('.page-submit').ajaxBusyProgress();

                    $.post(URL_MESSAGE_SEND,
                {
                    'memberString': memberString,
                    'messageTitle': title,
                    'messageText': text,
                    'needConfirm': needConfirm
                }, function (e) {
                    if (e.Status == "Success") {
                        var url = '/message/show?topicId={topicId}';
                        url = url.replace("{topicId}", e.Data.topicId);
                        $.redirectAndDisplayInfo(url, e);
                    } else {
                        $('.page-submit').ajaxBusyProgress('reset');
                    }
                });

                }
            };

            EzAccessor = realEzAccessor;
        }

        function chooseMembersCallBack(selectedMembers, selectedGroups) {
            // 将选中的用户加入收信人列表
            var members = [];
            var nameListUL = $('<ul/>')
            var nameListLI = nameListLI = $('<li/>').appendTo(nameListUL);
            var IdArray = [];

            if (selectedGroups != null) {
                // 这里先 hack 一下，如果选了组，那就是所有人
                nameListLI.append($('<p/>').html("所有人"))
                g_Var['recieverId'] = 'everyone';
            } else {

                $(selectedMembers).each(function (index, item) {
                    members.push(item.Name);
                    IdArray[IdArray.length] = item.Id;

                    nameListLI.append($('<p/>').html(item.Name))
                    g_Var['recieverId'] = IdArray.join(',');
                });
            }

            $('#divReceiver').empty().append(nameListUL);

            if (nameListLI.html() == "") {
                // 一个人都没有选中
                $('#divReceiver').hide();
            } else {
                $('#divReceiver').show();
            }
        }



        function checkForm() {
            // 判断标题长度
            if ($("#messageTitle").val().trim().length > 30) {
                var StatusInfo = { 'Status': 'Error', 'Info': '信件标题太长了，要求小于30个字符！', 'Data': null };
                $.displayStatusInfo(StatusInfo);
                return false;
            }

            //判断回复是否为空
            if ($("#messageContent").val().trim().length == 0) {
                var StatusInfo = { 'Status': 'Error', 'Info': '信件内容不能为空!', 'Data': null };
                $.displayStatusInfo(StatusInfo);
                return false;
            }

            //没有选择收件人
            if ($('#divReceiver').html().trim().length == 0) {
                var StatusInfo = { 'Status': 'Error', 'Info': '没有选择收件人!', 'Data': null };
                $.displayStatusInfo(StatusInfo);
                return false;
            }

            return true;

        }


        function initPageEvent() {

            // 选择收信人点击事件
            $('#lblChooseAddressee').userSelector({
                arrowPosition: 'after',
                selectorPanelClosed: function (e, arg) {
                    chooseMembersCallBack(arg.Users, arg.Groups);
                }
            });

            // 创建发送信件按钮的点击事件
            $('#btnSubmit').click(function () {
                var messageConfirm = checkForm();
                if (messageConfirm) {
                    EzAccessor.doSendMessage();
                }
            });

            $('#btnSendAndGo').click(function () {
                var messageConfirm = checkForm();
                if (messageConfirm) {
                    alert('这个功能还没实现。');
                }
            });

            //重设
            $('#btn-recover').click(function () {
                //清空标题和内容
                $('#messageTitle').clearFields();
                $('#messageContent').val("");
                //重设memberSelcet
                chooseMembersCallBack(null);
                //取消选中的信件需要确认
                $('#chk-need-confirm').attr('checked', '');
                EzTipsManager.hideTipBox('tipNeedConfirm');
                //隐藏标题
                $('#deleTtiel').parent().hide();
                $('#addTitel').show();
            });


            $('#addLine').click(function () {
                var height = $('#xhe0_iframe').height();
                if (height <= 420) {
                    $('#xhe0_iframe').height(height + 80);
                }
            });

            $('#subtractLine').click(function () {
                var height = $('#xhe0_iframe').height();
                if (height >= 200) {
                    $('#xhe0_iframe').height(height - 80);
                }
            });


            $('#chk-need-confirm').change(function () {
                var isNeedConfirm = $('#chk-need-confirm').attr('checked');
                if (isNeedConfirm) {
                    $('#pnlIsNeedConfirm').wrapTipBox({
                        tipId: '#tip_NeedConfirm',
                        direction: 'down'
                    });
                } else {
                    $('#pnlIsNeedConfirm').wrapTipBox('unwrapTipBox');
                }
            });

        }



        function initPage() {
            $('#messageContent').xheditor({ tools: 'Fontface,FontSize,Bold,Italic,Underline,FontColor,BackColor,Align,Emot' });

            $('#messageTitle').defaultText('在这里输入信件标题');

            $('#divReceiver').qtip({
                content: '他们将会收到这封信件。',
                position: {
                    my: 'left bottom',
                    at: 'top middle'
                }
            });

            $('#messageContent').focus();
        }


        $(function () {
            initPage();
            initPageEvent();
        });

    </script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head" class="page-head">
        <div id="links">
            <a href="/message/inbox" class="head">回到收件箱</a>
        </div>
        <img src="../../Content/icons/message_send.png" style="width: 48px; height: 48px;"
            alt="send" />
        <span class="title">写新信件</span>
    </div>
    <div id="page-body">
        <div>
            <span style="font-weight: bold;" id="lblChooseAddressee">选择收件人</span>
        </div>
        <div id="divReceiver" style="display: none;">
        </div>
        <div class="formBox" style="margin-top: 5px;">
            <div>
                <strong>标题</strong>
                <input type="text" id="messageTitle" style="width: 300px;" />
            </div>
            <div id="text" class="r">
                <textarea id="messageContent" style="width: 100%; height: 200px;"></textarea>
                <div style="float: right">
                    <div id="addLine" alt="send">
                        增加行数
                    </div>
                    <div id="subtractLine" alt="send">
                        减少行数
                    </div>
                    <script>
                        $(function () {
                            $("#addLine").button({
                                icons: {
                                    primary: "ui-icon-circle-plus"
                                },
                                text: false
                            });
                            $("#subtractLine").button({
                                icons: {
                                    primary: "ui-icon-circle-minus"
                                },
                                text: false
                            });
                        });
                    </script>
                </div>
            </div>
        </div>
        <div id="pnlIsNeedConfirm" class="content">
            <strong>信件是否需要被确认？</strong>
            <label id="Label1" for="chk-need-confirm">
                <input type="checkbox" id="chk-need-confirm" />需要</label></div>
    </div>
    <div class="page-submit">
        <button id="btnSubmit">
            发送
        </button>
        <script>
            $(function () {
                $("#btnSubmit").button({
                    icons: {
                        primary: "ui-icon-circle-arrow-e"
                    },
                    text: true
                });
            });
        </script>
        &nbsp;<a id="btnSendAndGo">发送并再写一封</a> &nbsp;或者&nbsp;<a id="btn-recover">重置</a>
    </div>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="PageShortcut" runat="server">
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageFoot" runat="server">
    <div id="tip_NeedConfirm" style="display: none;" class="tip-info">
        <h1>
            该信件需要被确认！</h1>
    </div>
</asp:Content>
